/*
 * @Author       : NaN 353362604@qq.com
 * @Date         : 2023-01-12 09:35:49
 * @LastEditTime : 2023-05-17 14:01:01
 * @LastEditors  : NaN 353362604@qq.com
 * @FilePath     : \AdminStration\src\Common\Modal\MoreModal\SelectModal.jsx
 * @Description  :
 */
import { Http } from '../../Api'
import request from 'umi-request'
import { useState } from 'react'
import { Button, message } from 'antd'
import { useRequest } from 'ahooks'
import { ProTable } from '@ant-design/pro-components'

// ? ~@ 选择模板
export const SelectModal = ({ CurrentData, actionRef, Off }) => {
    const [headers] = useState({ Authorization: sessionStorage.getItem('TOKEN') })
    // 分页信息
    const [paramsData, setParamsData] = useState()
    const { runAsync } = useRequest(Http, {
        manual: true,
        onSuccess: () => {
            actionRef.current.reload()
            message.success('添加成功!')
            Off()
        },
        onError: () => message.error('添加失败!'),
    })
    const Submit = data => {
        runAsync({ url: CurrentData.pos, data: CurrentData.assemblyData(data), path: [CurrentData.uuid] })
    }
    return (
        <ProTable
            actionRef={actionRef}
            columns={CurrentData.columns}
            rowSelection={CurrentData.hideInSelect ? false : { type: CurrentData?.isCheckbox ? 'checkbox' : 'radio' }}
            params={CurrentData.params}
            tableAlertOptionRender={({ selectedRows }) => (
                <Button type="link" onClick={() => Submit(selectedRows)}>
                    保存
                </Button>
            )}
            request={async (params = {}) => {
                if (CurrentData?.additional)
                    params[CurrentData.additional.key] = CurrentData[CurrentData.additional.value]
                delete params.current
                params.pageIndex = paramsData || 1
                return request(CurrentData.get, { params, headers }).then(res => {
                    return { data: res.list, total: res.total, success: true }
                })
            }}
            editable={{ type: 'multiple' }}
            pagination={{ pageSize: CurrentData.pageSize, showSizeChanger: true, onChange: e => setParamsData(e) }}
            columnsState={{ persistenceKey: 'pro-table-singe-demos', persistenceType: 'localStorage' }}
            rowKey="uuid"
            toolBarRender={e => CurrentData.options}
            search={CurrentData.hideInSearch ? false : true}
            headerTitle="筛选结果"
            scroll={{ y: 600 }}
        />
    )
}
